<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="utf-8" />
<title>收藏目录选择</title>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
<link href="${mvcPath}/dacp-view/aijs/css/ai.css" type="text/css" rel="stylesheet" />
<link rel="stylesheet" href="${mvcPath}/dacp-lib/ztree/zTreeStyle.css" type="text/css">
<style>
.media, .media-body {
  overflow: inherit;
  zoom: 1;
}

.list-group-item.active .dropdown-menu a {
  color: black;
}

small {
  font-size: 9px;
}
.fuelux .wizard {
  border-right: 0px;
  border-bottom: 1px solid #eee;
  box-shadow: 0 0px 0px;
  background-color: transparent;
  box-shadow: none;
  border-radius: 0px;
}

.wrapper {
  padding: 1px 15px 15px 15px;
}

.panel-body {
  padding: 2px 0px 5px 0px;
}
</style>
<script type="text/javascript" src="${mvcPath}/dacp-lib/jquery/jquery-1.10.2.min.js"></script>
<script type="text/javascript" src="${mvcPath}/dacp-lib/ztree/jquery.ztree.all-3.5.min.js"></script>
<script type="text/javascript" src="${mvcPath}/dacp-lib/bootstrap/js/bootstrap.min.js"></script>
<script type="text/javascript" src="${mvcPath}/dacp-lib/underscore/underscore-min.js"></script>
<script src="${mvcPath}/dacp-lib/cryptojs/aes.js" type="text/javascript"></script>
<script src="${mvcPath}/crypto/crypto-context.js" type="text/javascript"></script>
<script src="${mvcPath}/dacp-view/aijs/js/ai.core.js"></script>
<script src="${mvcPath}/dacp-view/aijs/js/ai.field.js"></script>
<script src="${mvcPath}/dacp-view/aijs/js/ai.jsonstore.js"></script>
<script src="${mvcPath}/dacp-view/aijs/js/ai.grid.js"></script>
<script src="${mvcPath}/dacp-view/aijs/js/ai.treeview.js"> </script>
<script src="${mvcPath}/dacp-res/catalog/common/js/ai.meta.widget.js"></script>

<style>
.media, .media-body {
  overflow: inherit;
  zoom: 1;
}

.list-group-item.active .dropdown-menu a {
  color: black;
}

small {
  font-size: 9px;
}
 
.wrapper {
  padding: 1px 15px 15px 15px;
}
 
a.plan {
  font-size: 14px;
}

a.task {
  font-size: 13px;
}

.toolbar {
  margin-top: 10px;
  margin-bottom: 10px;
}

.toolbar .form-control {
  display: inline;
  padding: 1px 2px;
}

.toolbar button {
  margin-left: 10px;
}
.panel .table-striped > thead th {
    background-color: #36B0C8;
}
td{
  word-break:break-all;
}
.form-horizontal .form-group {
    margin-left: 0px;
    margin-right: 0px;
}
.ztree .line {
  height: auto;
}
.panel-footer {
  height: 25px;
}
.panel-footer .btn-xs {
  padding:0px 5px;
  margin-top:-17px;
}
</style>

 
<script>
  var username = _UserInfo['username'];
  var xmlid = paramMap.XMLID;
  var objType = paramMap.OBJTYPE;
  var collectTo = paramMap.collectTo;

  var catacode; 
  var treeStore;
  var catalogTreeObj;

  var buildToolBar = function() {
    var toolbar = new AI.Toolbar({
      containerId:'toolbar-container',
      items:[ 
        { type : 'combox', label : '选择数据专辑', notNull : 'y',fieldName : 'catalog', storesql: getCatalogStoresql(), width : 200}
      ],
      fieldChange: function(fieldName, newVal) {
        catacode = newVal;
        catalogTreeObj = buildCatalogTree();
      }
    });
    $("#ul_toolbar-container").find('.navbar-label').css({
      "width": "80px"
    });
    $("#catalog option:eq(0)").remove();
    catacode = $("#catalog").val();
  }

  var getCatalogStoresql = function() {
    var catalogData = ai.getStoreData("select catalog_code, catalog_name, catalog_right, user_list, user_role, state from data_catalog_def where state = '发布' order by ordseq");
    catalogData = catalogData ? catalogData : [];
    var storesql = "";
    for(var i=0; i<catalogData.length; i++) {
      var catalogRight = catalogData[i].CATALOG_RIGHT;
      if(catalogRight == "私有") {
        var creater = catalogData[i].CREATER;
        var userList = catalogData[i].USER_LIST;
        var userRole = catalogData[i].USER_ROLE;
        var curUser = username;
        var hasRight = false;

        if(creater && curUser == creater) {
          hasRight = true;
        }
        if(userList && userList.indexOf(curUser) != -1) {
          hasRight = true;
        }
        if(userRole) {
          var curUserRoles = ai.getStoreData("select groupcode from metagroupuser where username = '" + curUser + "'");
          for(var j=0; j<curUserRoles.length; j++) {
            var role = curUserRoles[j].GROUPCODE;
            if(userRole.indexOf(role) != -1) {
              hasRight = true;
              break;
            }
          }
        } 
        if(!hasRight) {
          continue;
        }
      }
      storesql += catalogData[i].CATALOG_CODE + "," + catalogData[i].CATALOG_NAME + "|";
    }
    return storesql.substring(0, storesql.length-1);
  }

  var buildCatalogTree = function() {
    treeStore = new AI.JsonStore({
      sql: "select rulecode id, case when parentcode='' or parentcode is null then 'root' else parentcode end pId, rulename name, dbname from metadb_struct where dbname = '" + catacode + "' order by rulecode",
      table: "METADB_STRUCT",
      key: 'RULECODE,DBNAME',
      pageSize: -1
    });

    var setting = {
      view: {selectedMulti: false},
      check: {enable: false},
      data: {simpleData: {enable: true}},
      callback: {
        onClick: function(event, treeId, treeNode) {
          var rulecode = treeNode.id;
          var html = '<i class="glyphicon glyphicon-user"></i>确认收藏';
          var data = ai.getStoreData("select 1 from metadb_struct_obj where dbname = '" + catacode + "' and rulecode = '" + rulecode + "' and xmlid = '" + xmlid + "'");
          if(data.length == 0) {
            $("#confirm").html(html);
          }
          if(data.length > 0) {
            $("#confirm").html(html.replace("确认收藏", "取消收藏"));
          }
        }
      }
    };

    var treeObj;
    var zNodesNew = [];
    $("#catalogtree").empty();
    if(treeStore.root&&treeStore.root.length>0){
      for(var i=0;i<treeStore.root.length;i++){
        var val = treeStore.root[i];
        zNodesNew.push({id:val.ID,pId:val.PID,name:val.NAME});
      }
      $.fn.zTree.init($("#catalogtree"), setting, zNodesNew);
    } else {
      $("#catalogtree").append("暂无目录");
    }
    treeObj = $.fn.zTree.getZTreeObj("catalogtree");
    if(treeObj) {
      treeObj.expandAll(true); 
    }
    return treeObj;
  };

  var getFieldCNName = function(items, fieldname) {
    for(var i in items) {
      if(items[i].fieldName == fieldname) {
        return items[i].label;
      }
    }
  };

  var addCatalogTreeNode = function(parentcode) {
    var addItems = [
      {type:'textfield',label:'目录编号',fieldName:'RULECODE',id:'RULECODE', notNull : 'N'},
      {type:'textfield',label:'中文名称',fieldName:'RULENAME',id:'RULENAME', notNull : 'N'},
      {type:'textfield',label:'父目录编号',fieldName:'PARENTCODE',id:'PARENTCODE'},
      {type:'textarea',label:'备注',fieldName:'REMARK',id:'REMARK'}
    ];

    if(!parentcode) {
      addItems = [
        {type:'textfield',label:'目录编号',fieldName:'RULECODE',id:'RULECODE', notNull : 'N'},
        {type:'textfield',label:'中文名称',fieldName:'RULENAME',id:'RULENAME', notNull : 'N'},
        {type:'textarea',label:'备注',fieldName:'REMARK',id:'REMARK'}
      ];
    }

    var afterAdd = function(fieldVals) {
      var r = treeStore.getNewRecord();
      var notNullList = 'RULECODE,RULENAME';
      for(var fieldName in fieldVals) {
        if(notNullList.indexOf(fieldName) != -1) {
          if(!fieldVals[fieldName]) {
            alert(getFieldCNName(addItems,fieldName) + "不能为空！");
            return false;
          }
        }
        r.set(fieldName, fieldVals[fieldName]);
      }
      r.set("DBNAME", catacode);

      var newId = r.get("RULECODE");
      if(treeStore.getRecordByKey(newId + "," + catacode)) {
        alert("新的目录名称与已有重复，请重新输入！");
        return false;
      }
      treeStore.add(r);
      treeStore.commit();
      catalogTreeObj = buildCatalogTree();
    };

    ai.openFormDialog('新增目录', addItems, afterAdd);
    if(parentcode) {
      $("#_global_dlg_form #PARENTCODE").val(parentcode).attr("disabled", "true");
    }
  };

  var deleteCatalogTreeNode = function() {
    var selected = catalogTreeObj.getSelectedNodes();
    if(selected.length == 0) {
      alert("请选择要删除的节点!");
      return;
    }
    for(var i in selected) {
      var node = selected[i];
      if(node.isParent) {
        alert('该节点为父目录，请先删除所有的子目录！');
        return;
      }
      if(confirm("确认删除目录：" + node.name + " ？该目录的收藏记录将被删除")) {
        ai.executeSQL("delete from metadb_struct where rulecode = '" + node.id + "' and dbname = '" + catacode + "'");
        ai.executeSQL("delete from metadb_struct_obj where rulecode = '" + node.id + "' and dbname = '" + catacode + "'");
        catalogTreeObj = buildCatalogTree();
      }
    }
  };

  var confirmCollection = function(rulecode) {
    var isAdd = true;
    if($("#confirm").text().indexOf("取消") >= 0) {
      isAdd = false;
    }

    var obj = ai.getStoreData("select objname from metaobj where xmlid = '" + xmlid + "'")[0];
    if(!obj) {
      alert("metaobj表中无此对象相关信息，无法收藏或取消收藏");
      return false;
    }
    var objname = obj.OBJNAME;

    if(isAdd) {
      ai.executeSQL("insert into metadb_struct_obj (dbname, rulecode, xmlid, objtype, objname) values ('" + catacode + "', '" + rulecode + "', '" + xmlid + "', '" + objType + "', '" + objname + "')");
      return 1;
    }
    if(!isAdd) {
      ai.executeSQL("delete from metadb_struct_obj where dbname = '" + catacode + "' and rulecode = '" + rulecode + "' and xmlid = '" + xmlid + "'");
      return 0;
    }
  };
  
  $(document).ready(function() {
    if(collectTo == 'user') {
      $("#toolbar-container").hide();
      $("#header").html(username + "的个人目录列表").css({
        "line-height": 1.4,
        "height": "30px"
      });
      catacode = username;
    } else if(collectTo == 'catalog') {
      buildToolBar();
    }

    catalogTreeObj = buildCatalogTree();

    $("#addStruct").click(function() {
      var parentcode = null;
      var selected = [];
      if(catalogTreeObj) {
        selected = catalogTreeObj.getSelectedNodes();
      }
      if(selected.length > 1) {
        alert('只能选择一个节点！');
        return;
      } 
      if(selected.length == 1) {
        parentcode = selected[0].id;
      }
      addCatalogTreeNode(parentcode);
    });

    $("#addRootStruct").click(function() {
      addCatalogTreeNode();
    });

    $("#deleteStruct").click(function() {
      deleteCatalogTreeNode();
    });

    $("#confirm").click(function() {
      var rulecode = "";
      var selected = [];
      if(catalogTreeObj) {
        selected = catalogTreeObj.getSelectedNodes();
      }
      if(selected.length > 1) {
        alert('只能选择一个节点！');
        return;
      } 
      if(selected.length == 1) {
        rulecode = selected[0].id;
      }
      var result = confirmCollection(rulecode);
      var html = $(this).html();
      if(result == 1) {
        $(this).html(html.replace("确认收藏", "取消收藏"));
      } else if(result == 0) {
        $(this).html(html.replace("取消收藏", "确认收藏"));
      }
    });
  });


</script>
</head>
<body class="">
  <div class="container-fluid" >
    <div class="collapse navbar-collapse" style="padding-left: 0px; margin-bottom: -14px" id="toolbar">
      <div class="form-group" id="toolbar-container" style="margin-top: 1px;">
      </div>
    </div>
    <div class="row">
      <div class="col-sm-12" style="padding-left: 1px; padding-right: 1px; padding-top: 5px">
        <!-- <h5 id="header-one">选择数据专辑：<small id='tip'> </small> </h5> -->
        <section class="panel panel-info portlet-item" style="opacity: 1; z-index: 0;">
          <header class="panel-heading" style="height: 25px; line-height: 5px;" id="header"> 目录列表 </header>
          <div class="panel-body">
            <div class="tab-content">
              <footer class="panel-footer"> 
                <button id="addStruct" class="btn btn-primary btn-xs" style="margin-left: -10px;" enabled=""><i class="glyphicon glyphicon-user"></i>增加</button>&nbsp;
                <button id="addRootStruct" class="btn btn-primary btn-xs" enabled=""><i class="glyphicon glyphicon-user"></i>增加根目录</button>&nbsp;
                <button id="deleteStruct" class="btn btn-primary btn-xs" enabled=""><i class="glyphicon glyphicon-user"></i>删除</button>&nbsp;
                <button id="confirm" class="btn btn-danger btn-xs" enabled=""><i class="glyphicon glyphicon-user"></i>确认收藏</button>&nbsp;
              </footer>
              <div class="panel-body slim-scroll" style="width: 100%; height: 600px; padding: 0px; overflow: auto">
                <ul id="catalogtree" class="ztree"></ul>
              </div>
            </div>
          </div>
        </section>
      </div>
    </div>
  </div>
 
</body>
</html>